package com.example.sjly.ui.order;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TableLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.sjly.R;
import com.google.android.material.tabs.TabLayout;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

//orderfragment订单碎片
public class OrderFragment extends Fragment {

    private ImageView mBackTV, mMenuTV;
    private EditText mSearchET;
    private TableLayout mOrderTL;
    private ViewPager mOrderVP;

    private Context mContext;

    private String[] tabs = {"全部", "待付款", "待发货","待收货","待评价"};//标签栏的内容
    private List<OrderListFragment> fragmentList = new ArrayList<>();

    @Override
    public void onAttach(@NonNull @NotNull Context context) {//fragment绑定到activity
        super.onAttach(context);
        mContext = context;
    }

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {//在这个函数里创建了fragment视图

        View root = inflater.inflate(R.layout.fragment_order, container, false);//绑定布局文件


        OrderRoomDatabase orderRoomDatabase=OrderRoomDatabase.getInstance(mContext);


        mBackTV = root.findViewById(R.id.back_iv);
        mMenuTV = root.findViewById(R.id.menu_tv);
        mSearchET = root.findViewById(R.id.search_et);//搜索的编辑框
//        mOrderTL = root.findViewById(R.id.state_tab_tv);
//        mOrderVP = root.findViewById(R.id.order_vp);

        mBackTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=mSearchET.getText().toString();
                Order order=new Order(str,str);
                orderRoomDatabase.mDAO().insert(order);
            }
        });

        TabLayout tabLayout = root.findViewById(R.id.state_tab_tv);//标签栏的实现控件
        ViewPager viewPager = root.findViewById(R.id.order_vp);//分页的实现控件，viewPager和tabLayout绑定在一起，实现联动
        //添加tab
        for (int i = 0; i < tabs.length; i++) {
            tabLayout.addTab(tabLayout.newTab().setText(tabs[i]));
            fragmentList.add(OrderListFragment.newInstance(tabs[i]));
        }

        //viewPager的适配器
        viewPager.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tabs[position];
            }
        });

        //设置TabLayout和ViewPager联动
        tabLayout.setupWithViewPager(viewPager,false);

        return root;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


}
